"""
FIVEDASH(TM) -- General Purpose Accounting Software
Copyright (C) 2008  Obol Software Pty Ltd

This program is free software: you can redistribute it and/or modify
it. For terms and conditions, see fivedash/license/license.txt

"""

DEBUG = False

import scanning
from parseengine import Parse
import time
import fpformat

class Template:
        def __init__( self ):
                self.__token_list = None
                self.__filename = None
                
        def parseFile( self, filename ):
                orgstr = self.__getStrFrmFile( filename )
                self.parseString( orgstr )
#                Template.__cache_file[ filename ] = self.__node_list

                
        def parseString( self, orgstr ):
                token_list = scanning.createTokenList( orgstr )
                parse = Parse( token_list )
                starttime = time.time()
                self.__token_list = parse.process()
                usedtime = time.time() - starttime
                strlist = self.__token_list.debugprt()
                if not DEBUG:
                        from phunc.logger import AppLogger
                        logger = AppLogger().getHandler()
                        title = self.__filename != None and self.__filename or ''
                        logger.debug('--------------<parse_tree(%s)>--------------' % title )
                        for s in strlist:
                                logger.debug( '%s' %  s)
                        logger.debug('--------------</parse_tree(%s)>--------------' % title )
                        logger.debug('prase process used %ss ' % fpformat.fix(usedtime,5) )
                if DEBUG:
                        print '----------------<parse_tree>-----------------------------'
                        for s in strlist:
                                print s
                        print '----------------</parse_tree>-----------------------------'
                
                
        def __getStrFrmFile( self, filename ):
                self.__filename = filename
                starttime = time.time()
                file = open( filename, 'rb' )
                lines = file.readlines()
                file.close()
                usedtime = time.time() - starttime
                orgstr = ''.join( lines )
                if not DEBUG:
                        from phunc.logger import AppLogger
                        logger = AppLogger().getHandler()
                        logger.debug('read %s used %ss ' % (filename, fpformat.fix(usedtime,5)) )
                return orgstr
                
        
        def render( self, content ):
                #print content for debug
                if not DEBUG:
                        from phunc.logger import AppLogger
                        logger = AppLogger().getHandler()
                        logger.debug('----------------<content>-----------------------------')
                        for n in content.keys():
                                logger.debug( '%s:%s' % (n,content[n]) )
                        logger.debug('----------------</content>-----------------------------')
                if DEBUG:
                        print '----------------<content>-----------------------------'
                        for n in content.keys():
                                print '%s:%s' % (n,content[n])
                        print '----------------</content>-----------------------------'
                htmllist = self.__token_list.rander( content )
                html = ''.join( htmllist )
                #replace the None value to &nbsp;
                import re
                from phunc.logger import logger
                nb = re.compile('(?P<first><(?P<tag>[a-zA-Z]+)[^>]*>)\s*(?P<last></(?P=tag)>)')
                html = nb.sub(r'\g<first>&nbsp;\g<last>',html) 
                return html
                
